package org.liaohailong.library.widget.shopping.decoration;

import android.graphics.Rect;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

/**
 * Author: Victor
 * E-mail: liaohailong190@foxmail.com
 * Date: 2019/6/22 9:36
 * Description: StaggeredGridLayoutManager内部间隔
 * PS:使用此功能，需要给ItemView的宽度/高度设置Wrap_Content属性，否则会占用条目内容！！！
 */
public class ShoppingItemDecoration extends RecyclerView.ItemDecoration {
    private final int leftLinearMargin;
    private final int rightLinearMargin;
    private final int vLinearGap;

    private final int leftGridMargin;
    private final int rightGridMargin;
    private final int hGridGap;
    private final int vGridGap;


    /**
     * 布局中的间距设置
     *
     * @param leftLinearMargin  线性布局 左侧边距
     * @param rightLinearMargin 线性布局 右侧边距
     * @param vLinearGap        线性布局 垂直间距
     * @param leftGridMargin    网格（瀑布流） 左侧边距
     * @param rightGridMargin   网格（瀑布流） 右侧边距
     * @param hGridGap          网格（瀑布流） 水平间距
     * @param vGridGap          网格（瀑布流） 垂直间距
     */
    public ShoppingItemDecoration(
            int leftLinearMargin,
            int rightLinearMargin,
            int vLinearGap,
            int leftGridMargin,
            int rightGridMargin,
            int hGridGap,
            int vGridGap) {
        this.leftLinearMargin = Math.max(0, leftLinearMargin);
        this.rightLinearMargin = Math.max(0, rightLinearMargin);
        this.vLinearGap = Math.max(0, vLinearGap);
        this.leftGridMargin = Math.max(0, leftGridMargin);
        this.rightGridMargin = Math.max(0, rightGridMargin);
        this.hGridGap = Math.max(0, hGridGap);
        this.vGridGap = Math.max(0, vGridGap);
    }


    @Override
    public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);
        if (state.isPreLayout()) {
            return;
        }

        StaggeredGridLayoutManager layoutManager = (StaggeredGridLayoutManager) parent.getLayoutManager();
        if (layoutManager == null) {
            return;
        }
        StaggeredGridLayoutManager.LayoutParams layoutParams = (StaggeredGridLayoutManager.LayoutParams) view.getLayoutParams();
        if (layoutParams == null) {
            return;
        }
        RecyclerView.Adapter adapter = parent.getAdapter();
        if (adapter == null || adapter.getItemCount() <= 0) {
            return;
        }

        int left;
        int top = 0;
        int right;
        int bottom;
        /*
         * 以下部分是列表中部布局
         * */
        boolean fullSpan = layoutParams.isFullSpan();
        //线性条目
        if (fullSpan) {
            left = leftLinearMargin;
            right = rightLinearMargin;
            bottom = vLinearGap;
        }
        //网格条目
        else {
            int spanIndex = layoutParams.getSpanIndex();
            int spanCount = layoutManager.getSpanCount();
            left = spanIndex == 0 ? leftGridMargin : 0;
            bottom = vGridGap;
            right = spanIndex == spanCount - 1 ? rightGridMargin : hGridGap;
        }

        outRect.set(left, top, right, bottom);
    }
}